********************************************************************************
* BACKGROUND
********************************************************************************
/*	This code analyzes the impact of the Matter of A-B- decision on 
	removal decisions. It begins by visualizing observed removal rates around
	the decision date using a regression discontinuity plot. The code generates
	descriptive statistics and compares mean removal rates before and after the 
	decision. It then estimates a series of regression models within a 60-day 
	bandwidth around the decision date to test whether the effect of Matter of 
	A-B- varied by judge characteristics (e.g., gender, race/ethnicity, 
	appointing president) or by court-level diversity measures (e.g., percent 
	female or Hispanic judges). These models include interaction terms with 
	the treatment indicator and use margins and mlincom to calculate treatment
	effects and differences across subgroups. For continuous court diversity
	variables, effects are calculated at the median and one standard deviation
	above the median. Finally, the code prepares and exports a figure 
	visualizing the estimated differences in Matter of A-B- effects across 
	judge and court characteristics.*/
	
********************************************************************************
* READ IN DATA AND SET WORKING DIRECTORY TO SAVE OUTPUT FROM ANALYSIS
********************************************************************************

use matter_ab_replication_data	
cd "output"

********************************************************************************
* FIGURE: OBSERVED REMOVAL ORDER RATE AMONG ASYLUM SEEKERS
********************************************************************************

	bysort days_from_ab: egen mean_day_rem = mean(removal_decision)
	qui rdplot removal_decision days_from_ab if ///
		days_from_ab>-61 & days_from_ab<61 ///
		& app_asylum==1 & nationality!=0 & atty==1, genvars hide 
	
	twoway (scatter mean_day_rem p_comp_date if ///
		days_from_ab>-61 & ///
		days_from_ab<61 & rdplot_N>19 , mcolor(black%2) ///
		msymbol(smallcircle) msize(2-pt)  ///
		mfcolor(%50) mlcolor(%50)) ///
		(lfitci removal p_comp_date if days_from_ab>-61 & ///
		days_from_ab<1 & rdplot_N>19, lcolor(black) clwidth(medium) ///
		clpattern(solid) cmissing(y) fcolor(bluishgray%40) ///
		alcolor(bluishgray%40) alwidth(vvvthick) cmissing(y)) ///
		(lfitci  removal p_comp_date if days_from_ab>0 & ///
		days_from_ab<61 & rdplot_N>19, lcolor(black) clwidth(medium) ///
		clpattern(solid) fcolor(ltbluishgray%40) ///
		alcolor(bluishgray%40) alwidth(vvvthick) cmissing(y)), ///
		ytitle(Removal Rate) yscale(noline) ylabel(, tlcolor(black)) ///
		xtitle(" ") ///
		xscale(noline) xline(21346, extend) xlabel(, tlcolor(black)) ///
		xmtick(, tlcolor(black)) legend(off) ///
		xlabel( 21286 (30) 21406 , angle(forty_five) ///
		format(%tdMon_DD,_CCYY)) ///
		graphregion(lcolor(none) ilcolor(none)) plotregion(lcolor(black))	
			
	graph export ///
		matter_ab_line_w_CI.tif, ///
		width(3900) replace 
			
********************************************************************************
* DESCRIPTIVE STATISTICS 
********************************************************************************
	
log using matter_ab_descriptives.log, replace 
	
	sum removal_decision matter_of_ab_an nat_fe ///
		female ij_asian hispanic_ij apt_dem_pres ///
		percent_asian_14 percent_hispanic_14 percent_female_14 ///
		apt_dem_pres atty cube_rt_caseload ///
		English Spanish Other_Unknown_Lang ///
		Never_Detained Released Detained ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1
	
*mean by period 
	mean removal_decision ///
		if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1,over(matter_of_ab_an)
	
*total N by period 
	tab matter_of_ab_an if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1

log close 	
	
********************************************************************************
* REGRESSION MODELS W/60-DAY BANDWIDTH  
********************************************************************************

*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID  ///
		i.matter_of_ab_an ///
		1.`var'#1.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///		
		days_from_ab ///
		if days_from_ab>-61 & days_from_ab<61 & ///
		is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)

log using matter_ab_`var'.log, replace 
		
	margins `var'#matter_of_ab_an, post
			mlincom, clear 
			mlincom (2-1), ///
			stat(est se p) add ///
			rowname("AB `var'=0 DIF")
			mlincom (4-3), ///
			stat(est se p) add ///
			rowname("AB `var'=1")	
  			mlincom (4-3)-(2-1), ///
			stat(est se p) add ///
			rowname("DD")

log close 
}

*effect of percent female among male judges 
foreach var in  percent_female_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///		
		c.`var'##i.matter_of_ab_an ///
		days_from_ab if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
log using matter_ab_`var'.log, replace 
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
		at(`var'= (`median_val' `median_SD')) post 
		mlincom, clear 
		mlincom (2-1), ///
		stat(est se p) add ///
		rowname("Median")
		mlincom (4-3), ///
		stat(est se p) add ///
		rowname("Median+SD")		
  		mlincom (4-3)-(2-1), ///
		stat(est se p) add ///
		rowname("DD")

log close 
}

*effect of percent_hispanic and percent_asian among white judges 
foreach var in  percent_hispanic_14 percent_asian_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.apt_dem_pres#1.matter_of_ab_an ///		
		c.`var'##i.matter_of_ab_an ///
		days_from_ab if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)

log using  matter_ab_`var'.log, replace 
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 	
			mlincom (2-1), ///
			stat(est se p) add ///
			rowname("Median")
			mlincom (4-3), ///
			stat(est se p) add ///
			rowname("Median+SD")					
  			mlincom (4-3)-(2-1), ///
			stat(est se p) add ///
			rowname("DD")

log close 
}

********************************************************************************
* FIGURE: DIFFERENCES IN MATTER OF A-B- EFFECTS   
********************************************************************************

*categorical vars 
foreach var in female ij_asian hispanic_ij {
	
	logit removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		1.`var'#1.matter_of_ab_an ///
		days_from_ab ///
		if days_from_ab>-61 & days_from_ab<61 & ///
		is_missing==0 & IJ_pre_post_AB==1, cluster(nat_fe)
	
	margins `var', dydx(matter_of_ab_an) post 
			mlincom, clear 
			qui mlincom (2)-(1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
			
}
*effect of percent female among male judges 
foreach var in  percent_female_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		days_from_ab if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & female==0, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & female==0, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
			at(`var'= (`median_val' `median_SD')) post 
			mlincom, clear 				
  			mlincom (4-3)-(2-1), ///
			stat(est ul ll) add ///
			rowname("DD")
			gen `var'_e = _mlincom[1, 1] 
			gen `var'_ul = _mlincom[1, 2] 
			gen `var'_ll = _mlincom[1, 3] 
}

*effect of percent_hispanic and percent_asian among white judges 
foreach var in  percent_hispanic_14 percent_asian_14 {
	
	reg removal_decision ///
		i.lang i.custody_n  i.nat_fe ///
		cube_rt_caseload i.IJ_FE_ID ///
		i.matter_of_ab_an ///
		c.`var'##i.matter_of_ab_an ///
		days_from_ab if days_from_ab>-61 & days_from_ab<61 ///
		& is_missing==0 & IJ_pre_post_AB==1 & white_ij==1, cluster(nat_fe)
	
	sum `var' if days_from_ab>-61 & days_from_ab<61 & white_ij==1, d 
		local median_val = `r(p50)'
		local SD = `r(sd)' //rounding to integer
		local median_SD = `median_val' + `SD'
	
	margins matter_of_ab_an, ///
		at(`var'= (`median_val' `median_SD')) post 
		mlincom, clear 				
  		mlincom (4-3)-(2-1), ///
		stat(est ul ll) add ///
		rowname("DD")
		gen `var'_e = _mlincom[1, 1] 
		gen `var'_ul = _mlincom[1, 2] 
		gen `var'_ll = _mlincom[1, 3] 
}

*store output from margins 
	frame put /// 
		ij_asian_ll hispanic_ij_ll female_ll ///
		percent_asian_14_ll percent_hispanic_14_ll percent_female_14_ll ///
		ij_asian_ul hispanic_ij_ul female_ul ///
		percent_asian_14_ul percent_hispanic_14_ul percent_female_14_ul ///
		ij_asian_e hispanic_ij_e  female_e ///
		percent_asian_14_e percent_hispanic_14_e percent_female_14_e,   ///
		into(fig_frame2)
 
*make frame to plot stats 
	frame change fig_frame2
	duplicates drop 
	xpose, clear varname 
	rename (v1) (Matter_AB_effect)
	generate str var_id = substr( _varname , 1, strlen( _varname) - 2)
	gen yes_no=substr(_varname,-2,.) 

*set up data/labels 
	replace var_id ="6. Percent Female Court" if strpos( var_id, "percent_female")
	replace var_id ="5. Percent Hisp Court" if strpos( var_id, "percent_hispan")
	replace var_id ="3. Female" if strpos(var_id, "female")
	replace var_id ="1. Asian" if strpos( var_id, "ij_asian")
	replace var_id ="2. Hispanic" if strpos( var_id, "hispanic_ij")
	replace var_id ="4. Percent Asian Court" if strpos( var_id, "percent_asian")
	
	gen dif = Matter_AB_effect if yes_no=="_e"
	gen ul = Matter_AB_effect if yes_no=="ul"
	gen ll = Matter_AB_effect if yes_no=="ll"
   
   
	bysort var_id : egen Difference = max(dif)
	bysort var_id : egen U_CI = max(ul)   
	bysort var_id : egen L_CI = max(ll)   
	keep var_id Difference U_CI L_CI
	duplicates drop 
   
	encode var_id, gen(var_idn)
	label define graph_ab_difn ///
		1 "Asian IJ" ///
		2 "Hispanic IJ" ///
		3 "Female IJ" ///
		4 "Percent Asian Court +SD" ///
		5 "Percent Hispanic Court +SD" ///
		6 "Percent Female Court +SD" 
	
	label values var_idn graph_ab_difn

*actual figure command 
	twoway (rcap U_CI L_CI var_idn, sort) ///
	(scatter Difference var_idn, mcolor(black) ///
	msize(medium) msymbol(circle) mlabel(var_idn)  mlabcolor(black) ///
	mlabposition(9) mlabangle(vertical) mlabgap(small)), ///
    ytitle("Differences in {it: Matter of A-B-} Effects") ///
	yscale(noline) yline(0) ///
	ylabel(, ticks tlcolor(black)) xtitle("") ///
	xlabel(.85 1 2 3 4 4.15, nolabels noticks nogmin nogmax) ///
	legend(off) plotregion(lcolor(black) ilcolor(none))
	
	graph export ///
	matter_of_ab_dif.tif, ///
	width(3900) replace 